package com.allinfinance.starters.loader;

import com.allinfinance.starters.interfaces.StringConfigLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @desc: 组合加载器
 * @author: wujp
 * @create: 2025-01-14 19:02
 */
public class MultipartLoader implements StringConfigLoader {
    private Logger logger = LoggerFactory.getLogger(MultipartLoader.class);

    private List<StringConfigLoader> loaders;

    @Override
    public Map<String, String> load() {
        Map<String, String> configMap = new ConcurrentHashMap<>();
        logger.info("MultipartLoader load config start.");
        if (!CollectionUtils.isEmpty(loaders)) {
            loaders.stream().sorted(Comparator.comparingInt(StringConfigLoader::getOrder)).forEach(loader -> {
                configMap.putAll(loader.load());
            });
        }
        logger.info("MultipartLoader load config end. config size is {}", configMap.size());
        return configMap;
    }

    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
    }

    public List<StringConfigLoader> getLoaders() {
        return loaders;
    }

    public void setLoaders(List<StringConfigLoader> loaders) {
        this.loaders = loaders;
    }
}
